﻿ using System;
using System.Collections.Generic;
using System.Text;
using CMN.DAL;
using CMN.Entity;
using System.Data;
using System.Data.Common;
using CMN.Common;
namespace CMN.BLL
{
     /// <summary>
    ///  (文章)
    /// </summary>
    public class Module_Article
    {
        #region "Module_Article (文章 ) - Method"
        /// <summary>
        /// 新增/删除/修改 Module_ArticleEntity (文章)
        /// </summary>
        /// <param name="fam">Module_ArticleEntity实体类</param>
        /// <returns>-1:存储过程执行失败,-2:存在相同的主键,Insert:返回插入自动ID,Update:返回更新记录数,Delete:返回删除记录数</returns>
        public  Int32 InsertUpdateDelete(Module_ArticleEntity fam)
        {
            fam.Title=fam.Title.Replace("\"","");
            fam.Summary = fam.Summary.Replace("\"", "");
           int retrunInt = DALHelper.CMNHelper.Module_ArticleInsertUpdateDelete(fam);
           if (retrunInt > 0)
           {
               LuceneHelper helper = new LuceneHelper();
               if (fam.DataTable_Action == DataTableAction.Delete)
               {
                   helper.DeleteDocuments(fam.Id.ToString());
               }
               else if (fam.DataTable_Action == DataTableAction.Insert)
               {
                   if (!string.IsNullOrEmpty(fam.Tags))
                   {
                       List<Normal_TagsEntity> listTag = new List<Normal_TagsEntity>();
                       QueryParam qp = new QueryParam();
                       qp.Where = string.Concat("id in (", fam.Tags, ")");
                       int CountInt = 0;
                       Normal_Tags bllTags = new Normal_Tags();
                       listTag = bllTags.List(qp, out CountInt);
                       for (int i = 0; i < listTag.Count; i++)
                       {
                           if (i == listTag.Count)
                               fam.Tags = string.Concat(fam.Tags, listTag[i].TagName);
                           else
                               fam.Tags = string.Concat(fam.Tags, listTag[i].TagName, ",");
                       }

                   }
                   
                   helper.CreateIndex(fam);
               }
               else if (fam.DataTable_Action == DataTableAction.Update)
               {
                   if (fam.IsCreateIndex)
                   {
                       if (!string.IsNullOrEmpty(fam.Tags))
                       {
                           List<Normal_TagsEntity> listTag = new List<Normal_TagsEntity>();
                           QueryParam qp = new QueryParam();
                           qp.Where = string.Concat("id in (", fam.Tags, ")");
                           int CountInt = 0;
                          Normal_Tags bllTags=new Normal_Tags();
                          listTag = bllTags.List(qp,  out CountInt);
                           for (int i = 0; i < listTag.Count; i++)
                           {
                               if (i == listTag.Count)
                                   fam.Tags = string.Concat(fam.Tags, listTag[i].TagName);
                               else
                                   fam.Tags = string.Concat(fam.Tags, listTag[i].TagName, ",");
                           }

                       }
                       helper.UpdateIndex(fam);
                      // helper.DeleteDocuments(fam.Id.ToString());
                      // helper.CreateIndex(fam);
                   }
               }
           }
           return retrunInt;
        }
        /// <summary>
        /// 更新搜索列表
        /// </summary>
        /// <param name="fam"></param>
        /// <returns></returns>
        public Int32 UpdateSearch(Module_ArticleEntity fam)
        {
            return  DALHelper.CMNHelper.Module_ArticleUpdateSearch(fam);
        }
        /// <summary>
        /// 根据Id返回 Module_ArticleEntity实体类 单笔资料 (文章)
        /// </summary>
        /// <param name="Id">Id 文章ID</param>
        /// <returns>返回 Module_ArticleEntity实体类 Id为0则无记录</returns>
        public  Module_ArticleEntity  Disp(Int32 Id)
        {
            Module_ArticleEntity fam = null;
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.ReturnFields = " a.* ,c.TextContent,c.Remark,c.SearchUpdateTime,c.SearchContent ";
            qp.TableName = "  Module_Article a left join Module_ArticleContent c on a.id=c.id ";
            qp.Where = string.Concat("a.id=", Id.ToString());
            int RecordCount = 0;
            List<Module_ArticleEntity> lst =  DALHelper.CMNHelper.Module_ArticleList(qp,false,out RecordCount);

            if (lst!=null &&  lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }
        /// <summary>
        /// 根据Id返回 Module_ArticleEntity实体类 单笔资料 (文章)
        /// </summary>
        /// <param name="Id">Id 文章ID</param>
        /// <returns>返回 Module_ArticleEntity实体类 Id为0则无记录</returns>
        public Module_ArticleEntity Disp(string title)
        {
            Module_ArticleEntity fam = null;
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.ReturnFields = " a.* ,c.TextContent,c.Remark,c.SearchUpdateTime,c.SearchContent ";
            qp.TableName = "  Module_Article a left join Module_ArticleContent c on a.id=c.id ";
            qp.Where = string.Concat("a.title ='", title, "'");
            int RecordCount = 0;
            List<Module_ArticleEntity> lst = DALHelper.CMNHelper.Module_ArticleList(qp, false, out RecordCount);

            if (lst != null && lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }
        /// <summary>
        /// 根据Id返回 Module_ArticleEntity实体类 单笔资料 (文章)
        /// </summary>
        /// <param name="Id">Id 文章ID</param>
        /// <returns>返回 Module_ArticleEntity实体类 Id为0则无记录</returns>
        public Module_ArticleEntity DispSouceFrom(string url)
        {
            Module_ArticleEntity fam = null;
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.ReturnFields = " a.* ,c.TextContent,c.Remark,c.SearchUpdateTime,c.SearchContent ";
            qp.TableName = "  Module_Article a left join Module_ArticleContent c on a.id=c.id ";
            qp.Where = string.Concat("a.SourceFrom ='", url, "'");
            int RecordCount = 0;
            List<Module_ArticleEntity> lst = DALHelper.CMNHelper.Module_ArticleList(qp, false, out RecordCount);

            if (lst != null && lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }
        /// <summary>
        /// 根据Id返回 Module_ArticleEntity实体类 单笔资料 (文章)
        /// </summary>
        /// <param name="Id">Id 文章ID</param>
        /// <returns>返回 Module_ArticleEntity实体类 Id为0则无记录</returns>
        public Module_ArticleEntity DispRemark(Int32 Id)
        {
            Module_ArticleEntity fam = null;
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;

            qp.ReturnFields = " a.Id,Title,c.Remark ";
            qp.TableName = "  Module_Article a left join Module_ArticleContent c on a.id=c.id ";
            qp.Where = string.Concat(" a.id=", Id.ToString());
            int RecordCount = 0;
            List<Module_ArticleEntity> lst = DALHelper.CMNHelper.Module_ArticleList(qp, false, out RecordCount);

            if (lst != null && lst.Count > 0)
            {
                fam = lst[0];
            }
            return fam;
        }

        /// <summary>
        /// 根据Id返回 Module_ArticleEntity实体类 单笔资料 (文章)
        /// </summary>
        /// <param name="Id">Id 文章ID</param>
        /// <returns>返回 Module_ArticleEntity实体类 Id为0则无记录</returns>
        public  Module_ArticleEntity Disp(Int32 Id, Int32 pageIndex,Int32 pagingNum)
        {
            Module_ArticleEntity fam = null;
            if (pageIndex <= 0)
            {
                pageIndex = 1;
            }
            QueryParam qp = new QueryParam();
            qp.PageIndex = 1;
            qp.PageSize = 1;
            qp.ReturnFields = " a.* ,c.TextContent ,c.Remark,c.SearchUpdateTime,c.SearchContent";
            qp.TableName = "  Module_Article a left join Module_ArticleContent c on a.id=c.id ";
            qp.Where = string.Concat("a.id=", Id.ToString() ," and a.enabled=1 and a.ispass=1 ");
            int RecordCount = 0;
            List<Module_ArticleEntity> lst = DALHelper.CMNHelper.Module_ArticleList(qp, false, out RecordCount);
            if (pagingNum < 10)
            {
                pagingNum = Int32.MaxValue;
            }
            if (lst != null && lst.Count > 0)
            {
                fam = lst[0];


                string[] strs = null;
                if (fam.TextContent.IndexOf("$page$") > -1)
                {
                    strs = Utils.SplitString(fam.TextContent, "$page$");
                    fam.PageCount = strs.Length;
                    if (pageIndex > fam.PageCount)
                    {
                        pageIndex = fam.PageCount;
                    }
                    fam.TextContent = strs[pageIndex - 1];

                }
                else
                {

                    if (fam.TextContent.Length > pagingNum)
                    {


                        if (fam.TextContent.Length % pagingNum == 0)
                        {
                            fam.PageCount = fam.TextContent.Length / pagingNum;
                        }

                        else
                        {
                            fam.PageCount = fam.TextContent.Length / pagingNum + 1;
                        }
                        if (pageIndex > fam.PageCount)
                        {
                            pageIndex = fam.PageCount;
                        }

                        int startIndex = (pagingNum * (pageIndex - 1));
                        int endIndex = pagingNum;
                        int indexNum = pagingNum;
                        if (pageIndex >= 2)
                        {
                            if (fam.TextContent.Length > startIndex)
                            {
                                indexNum = fam.TextContent.IndexOf(">", (pagingNum * (pageIndex - 1)));
                                if (indexNum > -1)
                                    startIndex = indexNum+1;
                                indexNum = startIndex + pagingNum;
                            }
                            else
                                startIndex = 0;
                        }

                        if (fam.TextContent.Length > indexNum)
                        {
                            indexNum = fam.TextContent.IndexOf(">", indexNum);
                            if (indexNum > -1)
                                endIndex = indexNum+1;
                        }
                        else
                        {
                            endIndex = fam.TextContent.Length;
                        }

                        fam.TextContent = Utils.GetSubString(fam.TextContent, startIndex, endIndex - startIndex, "");
                    }
                }

                List<PageInfo> pageList = new List<PageInfo>();
                for (int i = 1; i <= fam.PageCount; i++)
                {
                    PageInfo pageInfo = new PageInfo();
                    pageInfo.PageNum = i;
                    pageList.Add(pageInfo);

                }
                fam.PageList = pageList;
            }



            return fam;
        }

        public  int  UpdateList(string updateValue, string where)
        {

            BLLHelper bll = new BLLHelper();
           return  bll.Update("Module_Article", updateValue, where);
        }
        /// <summary>
        /// 返回Module_ArticleEntity实体类的List对象 (文章)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="isReturnCountNum">是否返回记录总数</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>Module_ArticleEntity实体类的List对象(文章)</returns>
        public  List<Module_ArticleEntity> List(QueryParam qp, bool isReturnCountNum, out int RecordCount)
        {
            if (string.IsNullOrEmpty(qp.TableName))
            {
                qp.TableName = "Module_Article";
            }
            if (string.IsNullOrEmpty(qp.Orderfld))
            {
                qp.Orderfld = "Id desc";
            }
            if (string.IsNullOrEmpty(qp.ReturnFields))
            {
                qp.ReturnFields = "*";
            }
            if (string.IsNullOrEmpty(qp.NotInRowName))
            {
                qp.NotInRowName = "Id";
            }
            
            return DALHelper.CMNHelper.Module_ArticleList(qp, isReturnCountNum, out RecordCount);
        }
        public bool TitleIsHas(string title)
        {
            QueryParam qp = new QueryParam();
            qp.PageSize = 1;
            qp.Where = string.Concat("title ='", title, "'");
            qp.ReturnFields = "id";
            qp.IsReturnCount = false;
            int RecordCount = 0;
            List<Module_ArticleEntity> lst = List(qp, out RecordCount);
            if (lst != null && lst.Count > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 返回Module_ArticleEntity实体类的List对象 (文章)
        /// </summary>
        /// <param name="qp">查询类(非安全函数,传入参数请进行Sql字符串过滤)</param>
        /// <param name="RecordCount">返回记录总数</param>
        /// <returns>Module_ArticleEntity实体类的List对象(文章)</returns>
        public  List<Module_ArticleEntity> List(QueryParam qp, out int RecordCount)
        {
            if (string.IsNullOrEmpty(qp.TableName))
            {
                qp.TableName = "Module_Article";
            }
            if (string.IsNullOrEmpty(qp.Orderfld))
            {
                qp.Orderfld = "Id desc";
            }
            if (string.IsNullOrEmpty(qp.ReturnFields))
            {
                qp.ReturnFields = "*";
            }
            if (string.IsNullOrEmpty(qp.NotInRowName))
            {
                qp.NotInRowName = "Id";
            }
            return DALHelper.CMNHelper.Module_ArticleList(qp,qp.IsReturnCount, out RecordCount);
        }
        
        /// <summary>
        /// 批量删除((文章))
        /// </summary>
        /// <param name="idList">删除数组</param>
        /// <returns>删除的总个数</returns>
        public  int DelList(string idList)
        {
            


            Int32 rInt = -1;
            using (DbConnection conn = DALHelper.CMNHelper.CreateConnection())
            {
                conn.Open();
                DbTransaction dbTran = conn.BeginTransaction();
                try
                {

                    string strDel = string.Concat("delete from  Module_Article where Id in(", idList, ")");
                    string strDel2 = string.Concat("delete from  Module_ArticleContent where Id in(", idList, ")");
                   rInt= DALHelper.CMNHelper.ExecuteNonQuery(dbTran, CommandType.Text, strDel, null);
                    DALHelper.CMNHelper.ExecuteNonQuery(dbTran, CommandType.Text, strDel2, null);
                    dbTran.Commit();
                }
                catch (Exception ex)
                {
                    dbTran.Rollback();
                    throw new Exception(ex.Message);

                }
                finally
                {
                    conn.Close();
                }
            }

            return rInt;
        }
        #endregion
   }
 }
 